package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Creates a hashmap of configuration settings given any filename. The config file must have
 * the following format:
 * [#]PropertyName:Value;
 * @author Shahriar Haque
 *
 */
public class SettingsMap {
	
	private HashMap<String,String> map = new HashMap<String,String>();
	
	public SettingsMap(String fileName){
		try {
			Scanner file = new Scanner(new File(fileName));
			String line = "";
			int colon = 0;
			int scolon = 0;
			String property = "";
			String value = "";
			
			
			while(file.hasNextLine()){
				line = file.nextLine();
				if(line.length() <= 0) continue; // skip blank lines
				if(line.charAt(0) == '#') continue; // skip comments
				
				colon = line.indexOf(":");
				scolon = line.lastIndexOf(";");
				
				property = line.substring(0, colon).trim();
				value = line.substring(colon+1, scolon).trim();
				
				map.put(property, value);
				
			}
		
		
		
		} 

		
		catch (FileNotFoundException e) {
			System.err.println(fileName + " not found");
			System.exit(1);
		}
	}
	
	public String getValue(String propertyName) { return map.get(propertyName); }
	
	public static void main(String[] args){
		new SettingsMap("AppSettings.txt");
	}

}
